package cn.miao.photoprinter.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.miao.photoprinter.R;
import cn.miao.photoprinter.entity.ShoppingCart;

public class Cart2OrderAdapter extends BaseAdapter {

    private LayoutInflater infalter;
    private Context context;
    private List<ShoppingCart> shoppingCart = new ArrayList<ShoppingCart>();

    public Cart2OrderAdapter(Context context) {
        infalter = LayoutInflater.from(context);
        this.context = context;
    }
    
    public Cart2OrderAdapter(Context context, List<ShoppingCart> ShoppingCarts) {
        infalter = LayoutInflater.from(context);
        this.context = context;
        this.shoppingCart = ShoppingCarts;
    }

    @Override
    public int getCount() {
        return shoppingCart.size();
    }

    @Override
    public ShoppingCart getItem(int position) {
        return this.shoppingCart.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    public List<ShoppingCart> getAllCart() {
    	return shoppingCart;
    }
    
    public void removeShoppingCart(int position) {
    	this.shoppingCart.remove(position);
    	notifyDataSetChanged();
    }

    public void addAll(List<ShoppingCart> paths) {
        this.shoppingCart.clear();
        this.shoppingCart.addAll(paths);
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (null == convertView) {
            convertView = infalter.inflate(R.layout.item_cart2order, parent, false);
            holder = new ViewHolder();
            holder.tvShop = (TextView) convertView.findViewById(R.id.tvshop);
            holder.tvNum = (TextView) convertView.findViewById(R.id.tvNum);
            holder.tvPhotoinfo = (TextView) convertView.findViewById(R.id.tvphotoinfo);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ShoppingCart record = shoppingCart.get(position);
        holder.tvShop.setText(record.getShop());
        holder.tvNum.setText("" + record.getPhotocnt());
        holder.tvPhotoinfo.setText(record.getPhotoBrand() + record.getPhotoMatieral() + ", " + record.getPhotoSize());
        return convertView;
    }
    
    
    class ViewHolder {
    	TextView tvShop;
    	TextView tvPhotoinfo;
        TextView tvNum;
    }

}
